package org.squabble.service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.squabble.dao.ArticleDAO;
import org.squabble.domain.Account;
import org.squabble.domain.Article;
import org.squabble.domain.Tag;
import org.squabble.domain.property.SystemLongProperty;
import org.squabble.web.admin.NewsEditCommand;

@Service
public class ArticleService {

	private final Log log = LogFactory.getLog(this.getClass());

	@Autowired
	SystemService systemService;
	
	@Autowired
	AccountService accountService;

	@Autowired
	TagService tagService;

	@Autowired
	ArticleDAO articleDAO;

	public Article getArticle(Long id) {
		return articleDAO.getArticle(id);
	}

	@Transactional(propagation = Propagation.REQUIRED)
	public void saveArticle(NewsEditCommand nec, Account account) {
		Date now = new Date();
		Article article = new Article();
		article.setSourceDate(now);
		if (nec.getArticleId() != null) {
			article = this.getArticle(nec.getArticleId());
			article.setUpdateDate(now);
		}
		article.setAnnouncement(nec.isAnnouncement());
		article.setByline(nec.getByline());
		article.setContent(nec.getContent());
		article.setDeleted(nec.isDelete());
		Calendar date = Calendar.getInstance();
		date.setTime(nec.getDisplayDate());
		Calendar time = Calendar.getInstance();
		time.setTime(nec.getDisplayTime());
		date.set(Calendar.HOUR_OF_DAY, time.get(Calendar.HOUR_OF_DAY));
		date.set(Calendar.MINUTE, time.get(Calendar.MINUTE));
		article.setDisplayDate(date.getTime());
		article.setReplyable(nec.isReplyable());
		article.setTitle(nec.getTitle());
		Account writer = accountService.getAccount(nec.getUser());
		if (writer == null) {
			article.setWriter(account);
		} else {
			article.setWriter(writer);
		}
		Set<Tag> tags = article.getTags();
		tags = tagService.saveTags(nec.getTags(), tags);
		article.setTags(tags);
		articleDAO.saveArticle(article);
	}

	@Transactional(readOnly = true)
	public List<Article> getArticles() {
		List<Article> articles = articleDAO.getArticles();
		log.debug("Returning " + articles.size() + " articles");
		return articles;
	}

	@Transactional(propagation=Propagation.REQUIRED)
	public void delete(Long articleId) {
		Article article = articleDAO.load(articleId);
		articleDAO.delete(article);
	}

	public List<Article> getIndexArticles() {
		SystemLongProperty dispCountProp = (SystemLongProperty) systemService.getSystemProperty("indexDisplayCount"); 
		int dispCount = ((Long) dispCountProp.getValue()).intValue();
		List<Article> articles = articleDAO.getLatestArticles(dispCount);
		return articles;
	}

}
